import java.net.*;
import java.io.*;

public class CopyOfConnection extends Thread{
	private Peer peer;
	private int port, bytesRead ;
	//private ObjectOutputStream oops;
	//private ObjectInputStream oips;
	//private InputStream  is;
	//private File file;

	private Socket mySocket;

	public CopyOfConnection(Socket s) {
		mySocket = s;
		setPort(mySocket.getPort());
	}
	
	public void run() { //as its currently structured, will only allow one transfer at a time?
		try {
			while (GUI1.myProject.isRunning()) {
				BufferedReader looker = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
				if (looker.ready()) {
					String message = looker.readLine();
					message = message.trim();
					if(message.matches("File,.+")) {
						String[] messageParsed = message.split(",");
						sendFile(messageParsed[1]);
						System.out.println("made it here");
					}
				}
			}
			System.out.println("fuck this shit");
			mySocket.close();
		} catch (IOException e) {}
	}
	
	public void sendFile(String filename) throws IOException{
		File file = new File(filename);
		long fileSize = file.length();
		byte [] bytesArray  = new byte [(int) fileSize];
	      FileInputStream fis = new FileInputStream(file);
	      BufferedInputStream bis = new BufferedInputStream(fis);
	      bis.read(bytesArray,0,bytesArray.length);
	      OutputStream os = mySocket.getOutputStream();
	      System.out.println("Sending the file...");
	      os.write(bytesArray,0,bytesArray.length);
	      os.flush();
	      mySocket.close();
	}
	
	public void getFile(String filename, int fileSize) throws IOException, ClassNotFoundException{
		if(mySocket.isClosed()) {
			System.out.println("what the hell");
		}
		
		PrintWriter pw = new PrintWriter(mySocket.getOutputStream());
		pw.print("File," + filename + '\n');
		pw.flush();
				
		byte [] bytesArray   = new byte[fileSize];
		InputStream is = mySocket.getInputStream();
		System.out.println("receiving the requested file...");
		FileOutputStream fos = new FileOutputStream("testFileFoSho");
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		//is.re
		System.out.println("stuck here");

		bytesRead = is.read(bytesArray,0,bytesArray.length);
		int current = bytesRead;

		do {
			System.out.println(current + " " + bytesRead);
			bytesRead =
				is.read(bytesArray, current, (bytesArray.length-current));
			System.out.println("after read");
			if(bytesRead >= 0) current += bytesRead;
		} while(bytesRead > -1);
		
		System.out.println(bytesArray + " asdfsafdsa ");
		bos.write(bytesArray, 0 , current);
		bos.flush();
		@SuppressWarnings("unused")
		long end = System.currentTimeMillis();
		//System.out.println(end-start);
		bos.close();		
	}
	
	/*public FileList getFileList(){

		return null;
	}
	public  Peer getPeers(){

		return null;
	}*/
	
	public void sendPeers(){

	}
	
	public void sendFileList(){

	}
	
	public void checkSocket(){
		
	}
	
	public static void main(String[] args) {
		Socket s = null;
		while (s == null) {
		try {
			s = new Socket("209.65.57.20", 8888);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		}
		//e.printStackTrace();
		if(s.isConnected()) {
		}
		CopyOfConnection testConnection = new CopyOfConnection(s);
		//testConnection.start();
		
		try {
			testConnection.getFile("//media//USB Disk//o.png", 57735);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		try {
			s.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void setPeer(Peer peer) {
		this.peer = peer;
	}

	public Peer getPeer() {
		return peer;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public int getPort() {
		return port;
	}


}

